#ifndef SQLITE_H
#define SQLITE_H

#include <QSqlQuery>
#include <QSqlDatabase>
#include <QMap>
#include <QList>
#include <QDebug>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonObject>
#include <QSqlError>
#include <QJsonParseError>

#define welog_debug() (qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " ")

enum{
    NameRole = Qt::UserRole,
    SellRole,
    NumRole,
};

struct User_t{
    QString name = "";
    QString id = "";
    QString phone = "";
    QString passwd = "";
    QString option = "";
    int money;
};

struct Book_t{
    QString name;
    int num;
    int sell;
    QString img;
};

struct Lend_t{
    QString id;
    QString user;
    QString phone;
    QString book;
    QString lend;
    QString img;
    QString retun;
    int sell;
};
Q_DECLARE_METATYPE(Lend_t)

class Sqlite
{
public:
    static Sqlite *Get()
    {
        static Sqlite s;
        return &s;
    }

    Sqlite();

    void Open();

    void Insert_user(QString name, QString id, QString phone, QString passwd, int money=20, QString order="");
    User_t Select_user(QString id, QString passwd);
    User_t Select_user(QString id);
    void Update_userMoney(User_t user);
    void Update_userOrder(QString id, QString order);
    QString Select_userOrder(QString id);

    QList<Book_t> Select_book();
    Book_t Select_book(QString name);
    void Update_bookNum(QString name, int num);
    void Update_bookNum(QString name);
    void Update_book(Book_t book);
    void Insert_book(Book_t book);
    void Delete_book(QString name);

    void Insert_lend(User_t user, QString book, int sell);
    QList<Lend_t> Select_lend(QString id);
    QList<Lend_t> Select_lend();
    void Delete_lend(QString id, QString book, QString);
};

#endif // SQLITE_H
